home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / dev / c / libiconv_src.lha / src / ucs2swapped.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-07  |  969 b   |  41 lines

  1. /*
  2.  * UCS-2-SWAPPED = UCS-2-INTERNAL with inverted endianness
  3.  */
  4.  
  5. static int
  6. ucs2swapped_mbtowc (conv_t conv, wchar_t *pwc, const unsigned char *s, int n)
  7. {
  8.   /* This function assumes that 'unsigned short' has exactly 16 bits. */
  9.   if (sizeof(unsigned short) != 2) abort();
  10.  
  11.   if (n >= 2) {
  12.     unsigned short x = *(const unsigned short *)s;
  13.     x = (x >> 8) | (x << 8);
  14.     if (x >= 0xd800 && x < 0xe000) {
  15.       return RET_ILSEQ;
  16.     } else {
  17.       *pwc = x;
  18.       return 2;
  19.     }
  20.   }
  21.   return RET_TOOFEW(0);
  22. }
  23.  
  24. static int
  25. ucs2swapped_wctomb (conv_t conv, unsigned char *r, wchar_t wc, int n)
  26. {
  27.   /* This function assumes that 'unsigned short' has exactly 16 bits. */
  28.   if (sizeof(unsigned short) != 2) abort();
  29.  
  30.   if (wc < 0x10000 && !(wc >= 0xd800 && wc < 0xe000)) {
  31.     if (n >= 2) {
  32.       unsigned short x = wc;
  33.       x = (x >> 8) | (x << 8);
  34.       *(unsigned short *)r = x;
  35.       return 2;
  36.     } else
  37.       return RET_TOOSMALL;
  38.   } else
  39.     return RET_ILSEQ;
  40. }
  41.